from google.appengine.api import xmpp
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

#mensaje_recibido = message.body
#el que me mando el mensaje:    message.sender
#yoda esta online?:    xmpp.get_presence("master-yoda@appspot.com")
#mandarle hola a yoda:    xmpp.send_message("master-yoda@appspot.com","hola")
#invitar a yoda:     xmpp.send_invite("master-yoda@appspot.com")

p1 = "axelbrz@gmail.com"
p2 = "kevinalle@gmail.com"

def senderMail(s):
	return s.split("/")[0]

def primo(n):
	if n == 1: return False
	i = 2
	while i * i <= n:
		if n % i == 0: return False
		i += 1
	return True

class XMPPHandler(webapp.RequestHandler):
	def post(self):
		global p1, p2
		message = xmpp.Message(self.request.POST)
		sender = senderMail(message.sender)
		what = False
		primoMes = " es primo?"
		if message.body[-len(primoMes):] == primoMes:
			try:
				n = int(message.body[:-len(primoMes)])
				if primo(n):
					message.reply("si, " + str(n) + " es primo")
				else:
					message.reply("no, " + str(n) + " no es primo")
			except e, Exception:
				what = True
		else:
			what = True
		if what:
			message.reply("eh?? solo se si un numero es o no es primo!")
		
		
		
application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPPHandler)], debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
